﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Net;

namespace AteneoLibri.Utility
{
    public class Utility
    {

        public static string generateRecoveryCode(string param)
        {
            string guid = Guid.NewGuid().ToString();
            return hash(guid + param.GetHashCode().ToString());
        }

        public static string hash(string param)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(param, System.Configuration.ConfigurationManager.AppSettings["cryptography"]);
        }

        /// <summary>
        /// Resize a given image using the passed parameter
        /// </summary>
        /// <param name="originalImage"></param>
        /// <param name="widthInPixels"></param>
        /// <param name="heightInPixels"></param>
        /// <returns></returns>
        public static Bitmap DoResize(Bitmap originalImage, int widthInPixels, int heightInPixels)
        {
            Bitmap bitmap;
            try
            {
                bitmap = new System.Drawing.Bitmap(widthInPixels, heightInPixels);
                using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bitmap))
                {
                    // Quality properties
                    graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    graphic.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                    graphic.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                    graphic.DrawImage(originalImage, 0, 0, widthInPixels, heightInPixels);
                    return bitmap;
                }
            }
            finally
            {
                if (originalImage != null)
                {
                    originalImage.Dispose();
                }
            }
        }

        public static Bitmap ResizeImage(System.Drawing.Bitmap image, int width, int height, ResizeOptions resizeOptions)
        {
            float f_width;
            float f_height;
            float dim;
            switch (resizeOptions)
            {
                case ResizeOptions.ExactWidthAndHeight:
                    return DoResize(image, width, height);

                case ResizeOptions.MaxHeight:
                    f_width = image.Width;
                    f_height = image.Height;

                    if (f_height <= height)
                        return DoResize(image, (int)f_width, (int)f_height);

                    dim = f_width / f_height;
                    width = (int)((float)(height) * dim);
                    return DoResize(image, width, height);

                case ResizeOptions.MaxWidth:
                    f_width = image.Width;
                    f_height = image.Height;

                    if (f_width <= width)
                        return DoResize(image, (int)f_width, (int)f_height);

                    dim = f_width / f_height;
                    height = (int)((float)(width) / dim);
                    return DoResize(image, width, height);

                case ResizeOptions.MaxWidthAndHeight:
                    int tmpHeight = height;
                    int tmpWidth = width;
                    f_width = image.Width;
                    f_height = image.Height;

                    if (f_width <= width && f_height <= height)
                        return DoResize(image, (int)f_width, (int)f_height);

                    dim = f_width / f_height;

                    // Check if the width is ok
                    if (f_width < width)
                        width = (int)f_width;
                    height = (int)((float)(width) / dim);
                    // The width is too width
                    if (height > tmpHeight)
                    {
                        if (f_height < tmpHeight)
                            height = (int)f_height;
                        else
                            height = tmpHeight;
                        width = (int)((float)(height) * dim);
                    }
                    return DoResize(image, width, height);
                default:
                    return image;
            }
        }

        /// <summary>
        /// NON USATO
        /// Create or retrieve the default folder on the server
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void GetSetBookCoverDirectory(string path)
        {
            if (Directory.Exists(path))
                return; // if already exists then it will not create it
            else
                Directory.CreateDirectory(path); // if not created then it will create it.
        }

        public static string TrimTitle(string title, int numberOfChars)
        {
            string shortTitle = title;
            if (shortTitle.Length >= numberOfChars)
            {
                shortTitle = shortTitle.Substring(0, numberOfChars) + " ...";
            }
            return shortTitle;
        }

        /// <summary>
        /// NON USATO
        /// </summary>
        /// <param name="image"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static byte[] ImageToArray(Bitmap image, ImageFormat format)
        {
            using (MemoryStream mem = new MemoryStream())
            {
                mem.Position = 0;
                image.Save(mem, format);
                return mem.ToArray();
            }
        }

        public static string GetIP()
        {
            string hostName = System.Net.Dns.GetHostName();
            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostName);
            IPAddress[] addr = ipEntry.AddressList;

            return addr[addr.Length - 1].ToString();
        }
    }
}